<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    验证码：<input type="text" id="content"> <p id="num"></p>
    <input type="submit" value="验证" id="btn">
</body>
<script>
    function fun(min,max){
        if(min>max){
            var ls = min;
            min = max;
            max = ls;
        }
        return parseInt(Math.random()*(max-min+1) + min);
    }
    num.innerHTML = (fun(1000,9999));
    btn.onclick = function(){
        if(content.value == num.innerHTML){
            alert('验证成功')
        }else{
            alert('验证失败')
            num.innerHTML = fun(1000,9999);                // 验证失败时候 重新刷新验证码
            content.value = '';                         // 把input框的内容清空
        }
    }
    num.onclick = function(){
        this.innerHTML = fun(1000,9999)                         // 点击验证码元素 刷新验证码
    }
</script>
</html>